package com.lanou3g.musicenjoy.greendao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.lanou3g.musicenjoy.application.MusicApplication;
import com.lanou3g.musicenjoy.entity.greendao.DaoMaster;
import com.lanou3g.musicenjoy.entity.greendao.DaoSession;
import com.lanou3g.musicenjoy.entity.greendao.MusicInfoBeanDao;


/**
 * Created by dllo on 15/9/29.
 */
public class DaoSingleton {
    private static final String DATABASE_NAME = "music.db";//表名
    private volatile static DaoSingleton instance;

    private SQLiteDatabase db;
    private DaoMaster daoMaster;
    private DaoSession daoSession;
    private Context context;
    private DaoMaster.DevOpenHelper helper;
    private MusicInfoBeanDao musicInfoBeanDao;


    private DaoSingleton() {
        context = MusicApplication.getContext();
    }

    public static DaoSingleton getInstance() {
        if (instance == null) {
            synchronized (DaoSingleton.class) {
                if (instance == null) {
                    instance = new DaoSingleton();
                }
            }
        }
        return instance;
    }

    public DaoMaster.DevOpenHelper getHelper() {
        if (helper == null) {
            helper = new DaoMaster.DevOpenHelper(context, DATABASE_NAME, null);
        }
        return helper;
    }

    public SQLiteDatabase getDb() {
        if (db == null) {
            db = getHelper().getWritableDatabase();
        }
        return db;
    }

    public DaoMaster getDaoMaster() {
        if (daoMaster == null) {
            daoMaster = new DaoMaster(getDb());
        }
        return daoMaster;
    }

    public DaoSession getDaoSession() {
        if (daoSession == null) {
            daoSession = getDaoMaster().newSession();
        }
        return daoSession;
    }


    public MusicInfoBeanDao getMusicInfoBeanDao() {
        if (musicInfoBeanDao == null) {
            musicInfoBeanDao = getDaoSession().getMusicInfoBeanDao();
        }
        return musicInfoBeanDao;
    }


}
